/*   1:    */ package com.atomikos.icatch.admin.jmx;
/*   2:    */ 
/*   3:    */ import com.atomikos.icatch.admin.AdminTransaction;
/*   4:    */ import com.atomikos.icatch.admin.LogControl;
/*   5:    */ import com.atomikos.icatch.system.Configuration;
/*   6:    */ import java.util.ArrayList;
/*   7:    */ import java.util.List;
/*   8:    */ import javax.management.InstanceNotFoundException;
/*   9:    */ import javax.management.MBeanRegistration;
/*  10:    */ import javax.management.MBeanRegistrationException;
/*  11:    */ import javax.management.MBeanServer;
/*  12:    */ import javax.management.ObjectName;
/*  13:    */ 
/*  14:    */ public class JmxTransactionService
/*  15:    */   implements JmxTransactionServiceMBean, MBeanRegistration
/*  16:    */ {
/*  17:    */   private MBeanServer server;
/*  18:    */   private ObjectName[] beans;
/*  19:    */   private boolean heuristicsOnly;
/*  20:    */   
/*  21:    */   private JmxTransaction createMBean(AdminTransaction tx)
/*  22:    */   {
/*  23: 81 */     JmxTransaction ret = null;
/*  24: 82 */     switch (tx.getState())
/*  25:    */     {
/*  26:    */     case 0: 
/*  27: 84 */       ret = new JmxPreparedTransaction(tx);
/*  28: 85 */       break;
/*  29:    */     case 2: 
/*  30: 87 */       ret = new JmxHeuristicTransaction(tx);
/*  31: 88 */       break;
/*  32:    */     case 1: 
/*  33: 90 */       ret = new JmxHeuristicTransaction(tx);
/*  34: 91 */       break;
/*  35:    */     case 3: 
/*  36: 93 */       ret = new JmxHeuristicTransaction(tx);
/*  37: 94 */       break;
/*  38:    */     case 4: 
/*  39: 96 */       ret = new JmxHeuristicTransaction(tx);
/*  40: 97 */       break;
/*  41:    */     default: 
/*  42: 99 */       ret = new JmxDefaultTransaction(tx);
/*  43:    */     }
/*  44:103 */     return ret;
/*  45:    */   }
/*  46:    */   
/*  47:    */   private synchronized void unregisterBeans()
/*  48:    */   {
/*  49:    */     try
/*  50:    */     {
/*  51:109 */       if (this.beans != null) {
/*  52:110 */         for (int i = 0; i < this.beans.length; i++) {
/*  53:111 */           if (this.server.isRegistered(this.beans[i])) {
/*  54:112 */             this.server.unregisterMBean(this.beans[i]);
/*  55:    */           }
/*  56:    */         }
/*  57:    */       }
/*  58:    */     }
/*  59:    */     catch (InstanceNotFoundException e)
/*  60:    */     {
/*  61:116 */       throw new RuntimeException(e.getMessage());
/*  62:    */     }
/*  63:    */     catch (MBeanRegistrationException e)
/*  64:    */     {
/*  65:118 */       throw new RuntimeException(e.getMessage());
/*  66:    */     }
/*  67:120 */     this.beans = null;
/*  68:    */   }
/*  69:    */   
/*  70:    */   private AdminTransaction[] filterHeuristics(AdminTransaction[] txs)
/*  71:    */   {
/*  72:125 */     List ret = new ArrayList();
/*  73:126 */     for (int i = 0; i < txs.length; i++)
/*  74:    */     {
/*  75:127 */       AdminTransaction next = txs[i];
/*  76:128 */       switch (next.getState())
/*  77:    */       {
/*  78:    */       case 2: 
/*  79:130 */         ret.add(next);
/*  80:131 */         break;
/*  81:    */       case 1: 
/*  82:133 */         ret.add(next);
/*  83:134 */         break;
/*  84:    */       case 3: 
/*  85:136 */         ret.add(next);
/*  86:137 */         break;
/*  87:    */       case 4: 
/*  88:139 */         ret.add(next);
/*  89:    */       }
/*  90:    */     }
/*  91:144 */     return (AdminTransaction[])ret.toArray(new AdminTransaction[0]);
/*  92:    */   }
/*  93:    */   
/*  94:    */   public synchronized ObjectName[] getTransactions()
/*  95:    */   {
/*  96:154 */     unregisterBeans();
/*  97:155 */     LogControl logControl = JmxLogAdministrator.getInstance().getLogControl();
/*  98:157 */     if (logControl == null) {
/*  99:158 */       throw new RuntimeException("LogControl is null: transaction service not running?");
/* 100:    */     }
/* 101:160 */     AdminTransaction[] transactions = logControl.getAdminTransactions();
/* 102:161 */     if (this.heuristicsOnly) {
/* 103:162 */       transactions = filterHeuristics(transactions);
/* 104:    */     }
/* 105:164 */     this.beans = new ObjectName[transactions.length];
/* 106:166 */     for (int i = 0; i < transactions.length; i++) {
/* 107:    */       try
/* 108:    */       {
/* 109:168 */         this.beans[i] = new ObjectName("atomikos.transactions", "TID", transactions[i].getTid());
/* 110:    */         
/* 111:170 */         JmxTransaction bean = createMBean(transactions[i]);
/* 112:171 */         this.server.registerMBean(bean, this.beans[i]);
/* 113:    */       }
/* 114:    */       catch (Exception e)
/* 115:    */       {
/* 116:173 */         throw new RuntimeException(e.getMessage());
/* 117:    */       }
/* 118:    */     }
/* 119:177 */     return this.beans;
/* 120:    */   }
/* 121:    */   
/* 122:    */   public ObjectName preRegister(MBeanServer server, ObjectName name)
/* 123:    */     throws Exception
/* 124:    */   {
/* 125:188 */     this.server = server;
/* 126:    */     
/* 127:190 */     JmxLogAdministrator admin = JmxLogAdministrator.getInstance();
/* 128:191 */     Configuration.addLogAdministrator(admin);
/* 129:193 */     if (name == null) {
/* 130:194 */       name = new ObjectName("atomikos", "name", "TransactionService");
/* 131:    */     }
/* 132:195 */     return name;
/* 133:    */   }
/* 134:    */   
/* 135:    */   public void postRegister(Boolean arg0) {}
/* 136:    */   
/* 137:    */   public void preDeregister()
/* 138:    */     throws Exception
/* 139:    */   {
/* 140:213 */     unregisterBeans();
/* 141:    */   }
/* 142:    */   
/* 143:    */   public void postDeregister() {}
/* 144:    */   
/* 145:    */   public void setHeuristicsOnly(boolean heuristicsOnly)
/* 146:    */   {
/* 147:234 */     this.heuristicsOnly = heuristicsOnly;
/* 148:    */   }
/* 149:    */   
/* 150:    */   public boolean getHeuristicsOnly()
/* 151:    */   {
/* 152:241 */     return this.heuristicsOnly;
/* 153:    */   }
/* 154:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.admin.jmx.JmxTransactionService
 * JD-Core Version:    0.7.0.1
 */